package cn.jathlon.calendar;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarUtils {
	public static Calendar calendar = Calendar.getInstance();
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	public static String rightnow(){
		long rightnow = calendar.getTimeInMillis();
		return dateFormat.format(rightnow);
	}
    public static String getLastWeek() { 
        long rightnow = calendar.getTimeInMillis();
        long aweekbefore = 6 * 24 * 60 * 60 * 1000;
        return dateFormat.format(rightnow - aweekbefore);
    }
    public static String getFirstDayInMonth() { 
        calendar.set(getYear(),getMonth()-1, 1);//
        return dateFormat.format(calendar.getTime());
    }
    public static String getFirstDayInMonth(int month){ 
        calendar.set(getYear(), month-1, 1);// 
        return dateFormat.format(calendar.getTime());
    }
    public static String getLastDayInMonth(int month){ 
        calendar.set(getYear(), month, 1);  //
        return dateFormat.format(calendar.getTimeInMillis() - 24*60*60*1000);
    }
    public static String getFirstDayInQuart() { 
    	int month = getMonth();
    	if (month >= 1 && month <= 3)  month = 1;   
        if (month >= 4 && month <= 6)  month = 4;  
        if (month >= 7 && month <= 9)  month = 7;   
        if (month >= 10 && month <= 12)  month = 10;  
        calendar.set(getYear(),month-1, 1);   //
        return dateFormat.format(calendar.getTime());
    }
	public static String getFirstDayInYear(){ 
        calendar.set(getYear(), 0, 1);   //
		return dateFormat.format(calendar.getTime());
	}
    public static int getYear() { 
        return calendar.get(Calendar.YEAR);  
    }
    public static int getMonth() {  
        return calendar.get(Calendar.MONTH) + 1;
    }
    public static int getWeek() { 
    	return calendar.get(Calendar.DAY_OF_WEEK);
    }

	public static int getHoursofDay() {  
		return Calendar.HOUR_OF_DAY;
	}
	
	public static String [] getDaysBackward(int ndays) {  
		String [] daysBackward = new String[ndays];
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate;
		for (int i = 0; i < ndays; i++) {
			lastDate = Calendar.getInstance();
	        lastDate.add(Calendar.DATE, -1*i);  
			daysBackward[ndays - 1 - i] = df.format(lastDate.getTime()); 
		}
        return daysBackward;
	}
}